package beuthTaskList;

import java.util.Comparator;



public class TaskComperator implements Comparator<Task> {
	
	SortType sortType = SortType.TIME_LEFT_ASC;
	
	public TaskComperator(SortType sortType) {
		this.sortType = sortType;
	}
	
    @Override
    public int compare(Task o1, Task o2) {
    	switch (sortType) {
		case NAME_ASC : return sortByNameAsc(o1, o2);
		case NAME_DESC : return sortByNameDec(o1, o2);
		case TASK_PRIORITY_ASC : return sortByPriorityAsc(o1, o2);
		case TASK_PRIORITY_DESC : return sortByPriorityDec(o1, o2);
		case TASK_TYPE_ASC : return sortByTaskTypeAsc(o1, o2);
		case TASK_TYPE_DESC : return sortByTaskTypeDec(o1, o2);
		case CLOSING_DATE_ASC : return sortByClosingDateAsc(o1, o2);
		case CLOSING_DATE_DESC : return sortByClosingDateDec(o1, o2);
		case STATUS_PERCENT_ASC : return sortByPercentAsc(o1, o2);
		case STATUS_PERCENT_DESC : return sortByPercentDec(o1, o2);
		case TIME_SPEND_ASC : return sortByTimeSpendAsc(o1, o2);
		case TIME_SPEND_DESC : return sortByTimeSpendDec(o1, o2);
		case TIME_LEFT_ASC : return sortByTimeLeftAsc(o1, o2);
		case TIME_LEFT_DESC : return sortByTimeLeftDec(o1, o2);
		default:
			break;
		}
    	
        return o1.getName().compareTo(o2.getName());
    }
    
	private int sortByNameAsc(Task o1, Task o2) {
		return o1.getName().compareToIgnoreCase(o2.getName());
	}
	
	private int sortByNameDec(Task o1, Task o2) {
		return o2.getName().compareToIgnoreCase(o1.getName());
	}
	
	private int sortByPriorityAsc(Task o1, Task o2) {
		return o1.getTaskPriority().compareTo(o2.getTaskPriority());
	}
	
	private int sortByPriorityDec(Task o1, Task o2) {
		return o2.getTaskPriority().compareTo(o1.getTaskPriority());
	}
	
	private int sortByTaskTypeAsc(Task o1, Task o2) {
		return o1.getTaskType().toString().compareTo(o2.getTaskType().toString());
	}
	
	private int sortByTaskTypeDec(Task o1, Task o2) {
		return o2.getTaskType().toString().compareTo(o1.getTaskType().toString());
	}
	
	private int sortByClosingDateAsc(Task o1, Task o2) {
		return o1.getClosingDate().compareTo(o2.getClosingDate());
	}
	
	private int sortByClosingDateDec(Task o1, Task o2) {
		return o2.getClosingDate().compareTo(o1.getClosingDate());
	}
	
	private int sortByPercentAsc(Task o1, Task o2) {
		return Integer.valueOf(o1.getStatusPercent()).compareTo(o2.getStatusPercent());
	}
	
	private int sortByPercentDec(Task o1, Task o2) {
		return Integer.valueOf(o2.getStatusPercent()).compareTo(o1.getStatusPercent());
	}
	
	private int sortByTimeSpendAsc(Task o1, Task o2) {
		return Integer.valueOf(o1.getTimeSpend()).compareTo(o2.getTimeSpend());
	}
	
	private int sortByTimeSpendDec(Task o1, Task o2) {
		return Integer.valueOf(o2.getTimeSpend()).compareTo(o1.getTimeSpend());
	}
	
	private int sortByTimeLeftAsc(Task o1, Task o2) {
		return o1.timeLeft().compareTo(o2.timeLeft());
	}
	
	private int sortByTimeLeftDec(Task o1, Task o2) {
		return o2.timeLeft().compareTo(o1.timeLeft());
	}
	
}
